angular.module("crawlerApp").controller("loggerCtrl", function ($scope, $http, $state, $stateParams) {
    $scope.pageNum = 0;
    //todo  传入id
    $scope.config = $stateParams.config;
    $scope.start_time = '';
    $scope.end_time = '';
    $scope.param = {
        "query": {
            "match": {
                "taskid": $scope.config.id
            }
        },
        "size": 10,
        "from": 10 * $scope.pageNum,
        "sort": [
            {
                "@timestamp": {"order": "desc"}
            }
        ]
    };
    getLogger($scope.param);

    function getLogger(param) {

        $http.post("log/es/getLogger", param).then(function (response) {
            $scope.loggers = response.data.data;
            $scope.totalCount = response.data.total;
            if (Number(response.data.total) % 10 == 0) {
                $scope.totalPage = Number(response.data.total) / 10;
            } else {
                $scope.totalPage = Math.ceil(response.data.total / 10);
            }

        });


    };

    $scope.Previous = function (pageNum) {
        if (pageNum - 1 >= 0) {
            $scope.pageNum = pageNum - 1;
        }
        $scope.param.from = $scope.pageNum * 10;
        getLogger($scope.param);
    };

    $scope.Next = function (page) {
        if (page + 1 <= $scope.totalPage - 1) {
            $scope.pageNum = page + 1;
        }
        $scope.param.from = $scope.pageNum * 10;
        getLogger($scope.param);
    };

    $scope.goto = function (page) {
        if (Number(page) <= $scope.totalPage && Number(page) > 0) {
            $scope.pageNum = Number(page) - 1;
        }
        $scope.param.from = $scope.pageNum * 10;
        getLogger($scope.param);
    };


    $scope.search = function () {
        $scope.param = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date($scope.start_time).getTime(),
                                    "lt": new Date($scope.end_time).getTime()
                                }
                            }
                        }, {
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }
                    ]
                }

            },

            "size": 10,
            "from": 10 * $scope.pageNum,
            "sort": [
                {
                    "@timestamp": {"order": "desc"}
                }
            ]
        };
        getLogger($scope.param);

    };


    $('.form_datetime').datetimepicker({
        /*  language:  'zh-CN',*/
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });


    $scope.analysisLogger = function () {
        $state.go("logger-charts",{config:$scope.config });
    }

});
